package com.example.mrwang.fullscreen;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by MrWang on 2017/1/6.
 */
public class FullFragment extends Fragment {
    ViewWrapper wrapper;

    LinearLayout mLinFull;
    Activity mActivity;
    Button btn;

    boolean fla = true;
    int height;
    int width;
    int widthPx;
    int heightPx;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_full, container, false);
        mLinFull = (LinearLayout) view.findViewById(R.id.lin_full);
        btn = (Button) view.findViewById(R.id.btn);

        ViewGroup.LayoutParams layoutParams =
                mLinFull.getLayoutParams();

        width = layoutParams.width;
        height = layoutParams.height;

        WindowManager manager = mActivity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        widthPx = outMetrics.widthPixels;
        heightPx = outMetrics.heightPixels;

        wrapper = new ViewWrapper(mLinFull);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mActivity, "放大操作", Toast.LENGTH_SHORT).show();
                wrapper.setClearManager();
                if (fla) {
                    deFormation(wrapper, widthPx, heightPx);
                    fla = false;
                } else {
                    deFormation(wrapper, width, height);
                    fla = true;
                }

            }
        });
        return view;
    }

    private void deFormation(final ViewWrapper wrapper, int widthPx, int heightPx) {
        ObjectAnimator scaleAnimWidth = ObjectAnimator.ofInt(wrapper, "width", widthPx);
        ObjectAnimator scaleAnimHeight = ObjectAnimator.ofInt(wrapper, "height", heightPx);

        AnimatorSet set = new AnimatorSet();

        set.setDuration(500);
        set.play(scaleAnimWidth).with(scaleAnimHeight);
        scaleAnimWidth.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.d("tag", "onAnimationUpdate: " + animation.getAnimatedValue());

            }
        });

        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

                Log.d("tag", "onAnimationStart: " + animation.getStartDelay());
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        set.start();
    }


    //代理对象
    private static class ViewWrapper {
        private LinearLayout mTarget;
//        LinearLayout mLinFull;
        public ViewWrapper(LinearLayout target) {
            mTarget = target;
        }

        public void setClearManager() {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            lp.setMargins(0, 0, 0, 0);
            mTarget.setLayoutParams(lp);
        }

        public int getWidth() {
            return mTarget.getLayoutParams().width;
        }

        public int getHeight() {
            return mTarget.getLayoutParams().height;
        }

        public void setWidth(int width) {
            mTarget.getLayoutParams().width = width;
            mTarget.requestLayout();
        }

        public void setHeight(int height) {
            mTarget.getLayoutParams().height = height;
            mTarget.requestLayout();
        }
    }
}


     /*  *//*layoutParams.width = width2;
        layoutParams.height = height2;
        mLinFull.setLayoutParams(layoutParams);*//*

        ScaleAnimation animation = new ScaleAnimation(mActivity,null);
        animation.initialize(layoutParams.width,layoutParams.height,width2,height2);
        animation.setDuration(500);
        animation.setFillAfter(true);
        mLinFull.startAnimation(animation);

        //animation.startNow();*/
